package org.daochong.lang;

import java.util.Date;
import java.text.SimpleDateFormat;

public class JsonAccessException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	private int code;

	private String host;

	private String helpUrl;

	private boolean showException;

	private String content;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getHelpUrl() {
		return helpUrl;
	}

	public void setHelpUrl(String helpUrl) {
		this.helpUrl = helpUrl;
	}

	public boolean isShowException() {
		return showException;
	}

	public void setShowException(boolean showException) {
		this.showException = showException;
	}

	public JsonAccessException(int code, String message, Object... params) {
		super(StringUtils.value(Message.getInstance().getMessage(code, LocaleContext.getLocale(), params), message));
		this.code = code;
	}

	public JsonAccessException(int code, Object... params) {
		super(StringUtils.value(Message.getInstance().getMessage(code, LocaleContext.getLocale(), params)));
		this.code = code;
	}

	public JsonAccessException(int code, Throwable e, Object... params) {
		super(StringUtils.value(Message.getInstance().getMessage(code, LocaleContext.getLocale(), params),
				e.getMessage()), e);
		this.code = code;
	}

	public JsonAccessException(int code, String message, Throwable cause, Object... params) {
		super(StringUtils.value(Message.getInstance().getMessage(code, LocaleContext.getLocale(), params), message),
				cause);
		this.code = code;
	}

	public int getCode() {
		return code;
	}

	public String toString() {
		return "{error:{code:" + this.code + ",title=\"" + (this.getMessage()) + "\",time=\""
				+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "\""
				+ ((this.getCause() != null && this.isShowException())
						? ",exceptions:\"" + StringUtils.toString(this.getCause()) + "\"" : "")
				+ (this.getHost() != null ? ",host:\"" + getHost() + "\"" : "")
				+ (this.getContent() != null ? ",content:\"" + getContent() + "\"" : "")
				+ (this.getHelpUrl() != null ? ",helpUrl:\"" + getHelpUrl() + "\"" : "") + "}}";
	}

}
